package com.example.demo.mqserver.core;

import java.io.Serializable;
import java.util.UUID;

public class Message implements Serializable{
    private BasicProperties basicProperties = new BasicProperties();
    private byte[] body;

    // 消息在⽂件中对应的 offset 的范围, [offsetBeg, offsetEnd)
    // 从这个范围取出的 byte[] 正好可以反序列化成⼀个 Message 对象.
    // offsetBeg 前⾯的 4 个字节是消息的⻓度
    private transient long offsetBeg = 0;
    private transient long offsetEnd = 0;

    // 消息在⽂件中是否有效. 0x0 表⽰⽆效, 0x1 表⽰有效
    private byte isValid = 0x1;

    // 创建新的消息, 同时给该消息分配⼀个新的 messageId
    // routingKey 以参数的为准. 会覆盖掉 basicProperties 中的 routingKey
    public static Message createMessageWithId(String routingKey, BasicProperties basicProperties ,byte[] body ) {
        Message message = new Message();
        if (basicProperties != null) {
            message.basicProperties = basicProperties;
        }
        message.basicProperties.setMessageId("M-" + UUID.randomUUID());
        message.basicProperties.setRoutingKey(routingKey);
        message.body = body;
        return message;
    }


    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }
}




